package com.ui.containers
{
	import com.net.AssetData;
	import com.ui.controls.Tab;
	import com.ui.core.UIComponent;
	import com.ui.data.PanelData;
	import com.ui.data.TabbedPanelData;
	import com.ui.group.GToggleGroup;
	import com.ui.skin.SkinStyle;

	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @version 20091215
	 * @author Cafe
	 */
	public class TabbedPanel extends UIComponent
	{
		protected var _data:TabbedPanelData;
		protected var _group:GToggleGroup;
		protected var _viewStack:ViewStack;
		protected var _tabList:Array;
		protected var _tabView:Sprite;

		override protected function create():void
		{
			_tabList = new Array();
			_tabView = new Sprite();
			_group = new GToggleGroup();
			_viewStack = new ViewStack( _data.viewStackData );
			addChild( _viewStack );
			addChild( _tabView );
		}

		override protected function layout():void
		{
			_viewStack.x = 0;
			_viewStack.y = _data.tabData.height - 1;
		}

		private function initEvents():void
		{
			_group.selectionModel.addEventListener( Event.CHANGE, group_changeHandler );
			addEventListener( Event.ADDED_TO_STAGE, addToStage, false, 1 );
		}

		private function addToStage( event:Event ):void
		{
			_group.selectionModel.index = 0;
		}

		private function group_changeHandler( event:Event ):void
		{
			_viewStack.selectionModel.index = _group.selectionModel.index;
			_width = _viewStack.width;
			_height = _data.tabData.height + _viewStack.height - 1;
			dispatchEvent( event );
		}

		public function TabbedPanel( data:TabbedPanelData )
		{
			_data = data;
			super( _data );
			initEvents();
		}

		public function get selection():UIComponent
		{
			return _viewStack.selection;
		}

		public function get group():GToggleGroup
		{
			return _group;
		}

		public function addTab( title:String , component:UIComponent ):void
		{
			var tab:Tab = new Tab( _data.tabData );
			tab.text = title;
			var lastTab:Tab = _group.model.getLast() as Tab;
			if (lastTab)
			{
				tab.x = lastTab.x + lastTab.width+4;
				tab.y = 0;
			}
			_tabList.push( tab );
			_tabView.addChild( tab );
			_group.model.add( tab );
			_viewStack.add( component );
		}

		// 添加一个标签页
		public function addTabPage( strTab:String ):Panel
		{
			var panData:PanelData = new PanelData();
			panData.x = 0;
			panData.y = 0;
			panData.width = width;
			panData.height = height - _data.tabData.height;
			panData.bgAsset = new AssetData( SkinStyle.emptySkin );
			var pan:Panel = new Panel( panData );
			addTab( strTab, pan );
			return pan;
		}

		// -1:没有选中，0：第一个标签页
		public function setCurSelPage( index:int ):void
		{
			_group.selectionModel.index = index;
		}

		public function setTabName( index:int , title:String ):void
		{
			_tabList[index].text = title;
			tabLayou();
		}

		private function tabLayou():void
		{
			var length:int = _tabList.length;
			var data:Tab;
			var upData:Tab;
			for (var i:int = 1; i < length; i ++)
			{
				data = _tabList[i] as Tab;
				upData = _tabList[i - 1] as Tab;
				data.x = upData.width + upData.x;
			}
		}

		public function tabsMove( newX:int , newY:int ):void
		{
			_tabView.x = newX;
			_tabView.y = newY;
		}

		public function clear():void
		{
			for (var i:int = 0; i < _tabList.length; i ++)
			{
				_tabView.removeChild( _tabList[i] );
			}
			for (var j:int = 0; j < _viewStack.numChildren; j ++)
			{
				_viewStack.removeChildAt( 0 );
			}
			_tabList.length = 0;
			_group.model.source.length = 0;
		}
	}
}
